package com.zy.explore.wechat.service;

import com.zy.explore.wechat.base.mybatis.BaseDao;
import com.zy.explore.wechat.base.mybatis.BaseServiceImpl;
import com.zy.explore.wechat.domain.mybatis.dao.ExampleDao;
import com.zy.explore.wechat.domain.mybatis.entity.MybatisExampleEntity;
import com.zy.explore.wechat.frm.CommonIntIdFrm;
import com.zy.explore.wechat.frm.ExampleFrm;
import com.zy.explore.wechat.vo.ExampleVO;
import com.zy.explore.wechat.vo.PageVO;
import com.zy.explore.wechat.assembler.MybatisExampleAssembler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description Mybatis框架示例服务实现类
 *
 * @author ZY
 * @date 2021年05月19日20时46分19秒
 */
@Service
public class MybatisExampleService extends BaseServiceImpl<MybatisExampleEntity, Integer, Object> {

    @Resource
    private ExampleDao dao;

    /**
     * 定义抽象方法getDao获取当前实体Mapper对象
     *
     * @return 当前实体Mapper对象
     */
    @Override
    protected BaseDao<MybatisExampleEntity> getDao() {
        return this.dao;
    }

    /**
     * 新增数据
     *
     * @param frm 参数对象
     * @return 视图对象
     */
    public ExampleVO insert(ExampleFrm frm) {
        MybatisExampleEntity entity = MybatisExampleAssembler.convertFromFrm(frm);
        entity.setCreateTime(new Date());
        MybatisExampleEntity res = super.insertSelective(entity);
        if (res != null) {
            return MybatisExampleAssembler.convertToVO(res);
        }
        return null;
    }

    /**
     * 删除对象
     *
     * @param frm 参数对象
     * @return true:删除成功 false:删除失败
     */
    public Integer delete(CommonIntIdFrm frm) {
        return super.deleteByPrimaryKey(frm.getId());
    }

    /**
     * 更新对象
     *
     * @param frm 待更新的Frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public ExampleVO update(ExampleFrm frm) {
        MybatisExampleEntity entity = MybatisExampleAssembler.convertFromFrm(frm);
        entity.setUpdateTime(new Date());
        int res = super.updateByPrimaryKeySelective(entity);
        if (res == 1) {
            return MybatisExampleAssembler.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据ID查询数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public ExampleVO findById(CommonIntIdFrm frm) {
        MybatisExampleEntity entity = super.getByPrimaryKey(frm.getId());

        if (entity != null) {
            return MybatisExampleAssembler.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据条件查询数据集合
     *
     * @param frm 查询条件对象
     * @return 满足条件的数据集合
     */
    public List<ExampleVO> getList(ExampleFrm frm) {
        Example example = MybatisExampleAssembler.convertToExample(frm);
        List<MybatisExampleEntity> list = super.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            List<ExampleVO> retList = new ArrayList<>();
            list.forEach(x -> retList.add(MybatisExampleAssembler.convertToVO(x)));
            return retList;
        }
        return new ArrayList<>();
    }

    /**
     * 根据条件查询分页
     *
     * @param frm 查询条件
     * @return 满足条件的分页
     */
    public PageVO<ExampleVO> getPageInfo(ExampleFrm frm) {
        Example example = MybatisExampleAssembler.convertToExample(frm);
        PageInfo<MybatisExampleEntity> pageInfo = PageHelper.startPage(frm.getPageNum(), frm.getPageSize()).doSelectPageInfo(() -> super.selectByExample(example));
        PageRequest pageRequest = PageRequest.of(frm.getPageNum(), frm.getPageSize());
        Page<ExampleVO> retPage = new PageImpl<>(new ArrayList<>(), pageRequest, 0);
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())) {
            return new PageVO<>(retPage);
        }
        List<ExampleVO> retList = new ArrayList<>();
        pageInfo.getList().forEach(x -> retList.add(MybatisExampleAssembler.convertToVO(x)));
        retPage = new PageImpl<>(retList, pageRequest, pageInfo.getTotal());
        return new PageVO<>(retPage);
    }
}
